<?php

namespace App\Http\Middleware;

use App\Models\Database;
use App\Services\DBAPP;
use Closure;
use Illuminate\Http\Request;

class ApiDatabase
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        // header("Access-Control-Allow-Origin: *");//允许所有地址跨域请求
        // 验证数据库并且设置数据库
        $database = $request->database;
        $dbInfo = Database::where('key', $database)->first();
        if (!$dbInfo) {
            return ['code' => 4000, 'message' => '数据库不存在'];
        }
        // DBAPP::set_database($dbInfo->user_id, $dbInfo->name);
        DBAPP::set_other_database($dbInfo);
        return $next($request);
    }
}
